package nc.ui.so.m32.billui.view;

import java.awt.BorderLayout;
import java.io.File;

import javax.swing.JPanel;

import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;

import nc.ui.pub.beans.UIFrame;
import nc.ui.pub.beans.UIPanel;

public class PdfPreviewUI extends UIFrame{

	private static final long serialVersionUID = 1L;
	private UIPanel jContentPane = null;
	private JPanel viewerPanel = null;
	private SwingController controller = null;
	private File file;
	/**
	 * @param owner
	 */
	public PdfPreviewUI(File file) {
		super("PDF preview");
		this.file = file;
		
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(1100, 1000);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.UIPanel
	 */
	public UIPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new UIPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getViewerPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	public JPanel getViewerPanel() {
		if(viewerPanel == null) {
			SwingViewBuilder factory = new SwingViewBuilder(getController());
			viewerPanel = factory.buildViewerPanel();
			getController().getDocumentViewController().setAnnotationCallback(
					new org.icepdf.ri.common.MyAnnotationCallback(getController().getDocumentViewController()));
			getController().openDocument(file.getAbsolutePath());
		}
		
		return viewerPanel;
	}

	public SwingController getController() {
		if(controller == null) {
			controller = new SwingController();
		}
		return controller;
	}
	
	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
		if(!b) {
			dispose();
		}
	}

	@Override
	public void dispose() {
		
		super.dispose();
		getController().closeDocument();
		this.file.deleteOnExit();
	}
}
